﻿using SharPy.GameClasses;
using SharPy.Interface;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace SharPy.Parser.clCommands
{
    class SetBackground : clCommand
    {
        public static bool RegExpCheck(string s)
        {
            if (Regex.IsMatch(s, "^scene(.*)with(.*)", RegexOptions.IgnoreCase))
                return true;
            else if (Regex.IsMatch(s, "^scene(.*)", RegexOptions.IgnoreCase))
                return true;
            else
                return false;
        }


        public static clCommand Parse(string s)
        {
            if (Regex.IsMatch(s, "^scene(.*)with(.*)", RegexOptions.IgnoreCase))
            {
                MatchCollection m = Regex.Matches(s, @"scene(.*)with(.*)");
                return (new clCommand(clCommand.Name.SetBackground
                        , m[0].Groups[1].Value.Trim()
                        , m[0].Groups[2].Value.Trim()
                        ));
            }

            else if (Regex.IsMatch(s, "^scene(.*)", RegexOptions.IgnoreCase))
            {
                MatchCollection m = Regex.Matches(s, @"scene(.*)", RegexOptions.IgnoreCase);
                return (new clCommand(clCommand.Name.SetBackground
                        , m[0].Groups[1].Value.Trim()
                        , null
                        ));
            }
            else throw new ArgumentException();
        }

        public static async Task<int> CommandHandle(clCommand objCom, GamePage page)
        {
            Image imBackground = (Image)page.FindName("imBackground");

            imBackground.Source = await SharPy.GameClasses.clImages.getImage(objCom.sParameter[0]);
            clImages.DeleteOldImages(page, Windows.UI.Xaml.HorizontalAlignment.Center);
            clImages.DeleteOldImages(page, Windows.UI.Xaml.HorizontalAlignment.Left);
            clImages.DeleteOldImages(page, Windows.UI.Xaml.HorizontalAlignment.Right);
            
            if (objCom.sParameter[1] != null)
            {
                switch (objCom.sParameter[1])
                {
                    case "fade":
                        clAnimations.FadeIn(page, imBackground);
                    break;
                    default:
                        break;
                }
            }
            return 0;
        }
    }
}
